package com.universest.swordholder.data.repository
import com.universest.swordholder.data.AppDatabase
import com.universest.swordholder.data.dao.User
import com.universest.swordholder.data.dao.UserDao
import kotlinx.coroutines.flow.Flow

class UserRepository(val database: AppDatabase) {

    private val userDao: UserDao = database.userDao()

    // 按ID查询用户（返回Flow，供Compose观察）
    fun getUserById(userId: Int): Flow<User?> {
        return userDao.getUserById(userId)
    }

    // 按用户名查询用户（新增）
    fun getUserByUserName(userName: String): Flow<User?> {
        return userDao.getUserByUserName(userName)
    }

    // 获取所有用户（返回Flow）
    fun getAllUsers(): Flow<List<User>> {
        return userDao.getAllUsers()
    }

    // 添加用户（挂起函数，需在协程中调用）
    suspend fun addUser(user: User): Long {
        return userDao.addUser(user)
    }

    // 更新用户
    suspend fun updateUser(user: User) {
        userDao.updateUser(user)
    }

    // 删除用户
    suspend fun deleteUser(user: User) {
        userDao.deleteUser(user)
    }

    // 按ID删除用户
    suspend fun deleteUserById(userId: Int) {
        userDao.deleteUserById(userId)
    }
}
